﻿using System;
using System.Data;
using System.Text;
using Besture.Bll;
using Besture.Common;


namespace Besture.UI.Admin.Common
{
    public partial class ArticleClassTree : System.Web.UI.Page
    {
        public string _json = string.Empty;

        public string _SelectClassID = string.Empty;

        public bool showCurrentPart;


        protected void Page_Load(object sender, EventArgs e)
        {
            //当前栏目
            _SelectClassID = StringHelper.Query("selectClassID");
            //是否仅显示当前栏目
            showCurrentPart = Convert.ToBoolean(StringHelper.Query("showCurrentPart"));

            string CurrentPartID = StringHelper.Query("CurrentPartID");

            string sql =
                string.Format("SELECT p.*,t.[Value] FROM F_GetPart({0}) p LEFT JOIN TBDictionary t ON p.TypeID = t.ID",showCurrentPart?_SelectClassID:"0");
            if(showCurrentPart)
            {
                sql =
                    string.Format(
                        @"  
                    SELECT ID,NAME AS PartName,ParentID,0 as TypeID ,0 as LEVEL,0 AS [value] FROM TBPart WHERE ID = {0}
                    UNION ALL 
                    SELECT p.*,t.[Value] FROM F_GetPart({0}) p LEFT JOIN TBDictionary t ON p.TypeID = t.ID",
                        CurrentPartID);
            }

            DataTable table = new QueryDac().GetDataTable(sql);

            _json = ZtreeJson(table);

            
        }

        public string ZtreeJson(DataTable table)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (StringHelper.Query("needTopClass") == "1")
            {
                stringBuilder.Append("{id:0, pId:0, name: '顶级栏目', open:true},");
            }
            foreach (DataRow row in table.Rows)
            {
                stringBuilder.Append("{id:" + row["ID"] + ", pId:" + row["ParentID"] + ", name: '" + row["PartName"] + "', open:true,typeid:'" + row["Value"] + "'},");
            }
            string json = string.Format(@"[{0}];", stringBuilder.ToString().TrimEnd(','));
            return json;
        }
        //,typeid='"+row["TypeID"]+"'
    }
}